package com.smartscreen.baseinfomanage.controller;

import com.smartscreen.baseinfomanage.entity.School;
import com.smartscreen.baseinfomanage.service.SchoolService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/base")
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    /**
     * 获取所有学校信息
     */
    @GetMapping("/schools")
    public ApiResponse<List<School>> getAllSchools() {
        List<School> schools = schoolService.getSchoolInfo();
        return ApiResponse.success("获取成功", schools);
    }

    /**
     * 获取单个学校信息（兼容旧接口）
     * 返回第一个学校的信息
     */
    @GetMapping("/school")
    public ApiResponse<School> getSchoolInfo() {
        List<School> schools = schoolService.getSchoolInfo();
        if (schools != null && !schools.isEmpty()) {
            return ApiResponse.success("获取成功", schools.get(0));
        } else {
            return ApiResponse.error("暂无学校信息");
        }
    }

    /**
     * 根据ID获取学校信息
     */
    @GetMapping("/schools/{id}")
    public ApiResponse<School> getSchoolById(@PathVariable Long id) {
        List<School> schools = schoolService.getSchoolInfo();
        School school = schools.stream()
                .filter(s -> s.getId().equals(id))
                .findFirst()
                .orElse(null);
        
        if (school != null) {
            return ApiResponse.success("获取成功", school);
        } else {
            return ApiResponse.error("未找到指定学校信息");
        }
    }

    /**
     * 创建学校信息
     */
    @PostMapping("/schools")
    public ApiResponse<School> createSchool(@RequestBody School school) {
        // 清空ID，让数据库自动生成
        school.setId(null);
        School createdSchool = schoolService.updateSchoolInfo(school);
        return ApiResponse.created("创建成功", createdSchool);
    }

    /**
     * 更新学校信息
     */
    @PutMapping("/schools/{id}")
    public ApiResponse<School> updateSchoolById(@PathVariable Long id, @RequestBody School school) {
        school.setId(id);
        School updatedSchool = schoolService.updateSchoolInfo(school);
        return ApiResponse.success("更新成功", updatedSchool);
    }

    /**
     * 更新学校信息（兼容旧接口）
     * 如果没有提供ID，则更新第一个学校
     */
    @PutMapping("/school")
    public ApiResponse<School> updateSchoolInfo(@RequestBody School school) {
        School updatedSchool = schoolService.updateSchoolInfo(school);
        return ApiResponse.success("更新成功", updatedSchool);
    }

    /**
     * 删除学校信息
     */
    @DeleteMapping("/schools/{id}")
    public ApiResponse<Void> deleteSchool(@PathVariable Long id) {
        // 这里需要在Service层添加删除方法
        // schoolService.deleteSchool(id);
        return ApiResponse.success("删除功能待实现", null);
    }

    /**
     * 获取学校数量
     */
    @GetMapping("/schools/count")
    public ApiResponse<Integer> getSchoolCount() {
        List<School> schools = schoolService.getSchoolInfo();
        int count = schools != null ? schools.size() : 0;
        return ApiResponse.success("获取成功", count);
    }
}